package qj.app.trader;
public class ReaderIndex {
	int fix = 0;
	int expand = 0;
	private String content;
	public ReaderIndex(String content) {
		this.content = content;
	}
	public String expandContent() {
		return content.substring(fix, expand);
	}
	public boolean isComplexWord() {
		return isComplexWord(expandContent());
	}
	public static boolean isComplexWord(String content) {
		boolean hasLetter = false;
		boolean hasNonLetter = false;
		for (int i = 0; i < content.length(); i++) {
			char c = content.charAt(i);
			if (Character.isWhitespace(c)) {
				continue;
			}
			if (Character.isLetter(c)) {
				if (hasNonLetter) {
					return true;
				} else {
					hasLetter = true;
				}
			} else {
				if (hasLetter) {
					return true;
				} else {
					hasNonLetter = true;
				}
			}
		}
		return false;
	}
	public boolean isMark() {
		return Words.isMark(expandContent());
	}
	public void fix() {
		fix = expand;
		removeHeadSpaces();
	}
	public void reset() {
		expand = fix;
	}

	public boolean isNormalWord() {
		return Words.isWord(expandContent());
	}
	public boolean isEnd() {
		return fix == content.length();
	}
	public boolean expandNormalWord() {
		return expandWord(false);
	}
	public boolean expandWord() {
		return expandWord(true);
	}
	public boolean expandWord(boolean acceptMark) {
		boolean meetingWord = false;
		boolean meetingMark = false;
		int i=0;
		for (;i<content.length() - expand;i++) {
			char c = content.charAt(expand + i);
			if (Words.isWord(c)) {
				if (!acceptMark && meetingMark) { // Met word after mark when not accept mark
					return false;
				}
				meetingWord = true;
			} else if (Words.isMark(c)) {
				meetingMark = true;
			} else if (!meetingWord && !meetingMark) { // Meet space when not met content
				continue;
			} else { // Meet space when met content
				break;
			}
		}
		if (meetingMark && !meetingWord && !acceptMark) {
			return false;
		}
		if (meetingWord) {
			// Done
			expand += i;
			
			if (meetingMark) {
				// Roll back any trail marks
				rollbackTrailMarks();
			}
			return true;
		} else if (meetingMark) {
			expand += i;
			return true;
		}
		return false;
	}
	private void rollbackTrailMarks() {
		for (int j = 0; ; j++) {
			if (Words.isMark( content.charAt(expand - j -1))) {
				continue;
			} else {
				expand-=j;
				break;
			}
		}
	}
	private void removeHeadSpaces() {
		for (int j = 0; j<content.length() - fix ; j++) {
			if (Character.isWhitespace( content.charAt(fix + j))) {
				continue;
			} else {
				fix+=j;
				expand=Math.max(expand, fix);
				break;
			}
		}
	}
	
	static class Test {
		public static void main(String[] args) {

			String content = "Hyundai Santafe Xe 7 chổ, Máy xăng 2.4DOHC";
			System.out.println(isComplexWord(","));
		}
		
	}

	
}